<?php
/**
 * 微笑云订票控制器
 * Author: 孙武 QQ:1228746736
 * Date: 2017/10/28
 * Time: 20:17
 */


namespace Home\Controller;

class OrderController extends BaseOrderController
{

    private $_M;//模型

    public function __construct()
    {
        parent::__construct();
        $this->_M = new \Home\Model\OrderModel();
    }


    /**
     * 添加乘客的提交页面
     * v2.0.0 逻辑更新
     */
    public function bookTicket()
    {


        /*检查是否有未提交的订单
        有:跳到订单详情页面
        无:到本页面
        */

        if (session('ticketId') == session('order')['od_addr_id']) {
            $this->redirect('detailTicket');
        } else {
            /*显示添加乘客返回来的用户*/
            if (!empty($_POST)) {

                $selectedPsrPostData = $_POST['checkbox'];
                //选中的用户的id
                $psrIds = array();
                foreach ($selectedPsrPostData as $k => $v) {
                    $psrIds[] = $k;
                }
                //数组转换为字符串
                $psrIds = implode(',', $psrIds);

                //根据选中用户的id，获取用户的显示信息
                $sql = "select * from bus_passenger where psr_id in ($psrIds)";
                $psrInfo = D()->query($sql);
                if (!$psrInfo) {
                    $this->err('服务器繁忙，请稍后再试');
                    exit;
                }
                $this->assign("psrInfo", $psrInfo);
            }

            /*处理车票信息*/
            if ($_GET['ticketId']) {

                //todo 用户已经选定了票，持久化此信息
                session('ticketId', I('get.ticketId'));
                $ticketInfo = D('addr')->find(session('ticketId'));

                //处理时间戳
                $ticketInfo['addr_depart_time'] = date("Y-m-d H:i", $ticketInfo['addr_depart_time']) . " " . changeNumberToweekday(date('w', $ticketInfo['addr_depart_time']));
                $this->assign("ti", $ticketInfo);
            }

            /*获取用户基本信息*/
            $usrInfo = D('user')->field('usr_name,usr_tel')->find(session('userId'));
            $this->assign("usrInfo", $usrInfo);
            $this->assign("tickedId", session('ticketId'));
            $this->assign("insur", 3);
            $this->display();
        }

    }


    /**
     * 订单详情页面
     */
    public function detailTicket()
    {


        $addrId = session('ticketId');//车次id

        /*检查是否是由订票信息页面跳转过来的
           有:获取持久化信息,展示订单详情页面
           无:搜集订单信息
       */

        if (session('ticketId') == session('order')['od_addr_id']) {
            if (C('HOME_DEBUG')) {
                echo "历史session订单:";
                dump(session('order'));
            }
            $this->assign("order",session('order'));
        } else {
            /*收集用户的信息*/
            $dbData = $this->_getTicketDetail($addrId);

            if (($errMsg = $this->checkDataIsLeague($dbData)) !== true) {
                $this->err($errMsg);
                exit;
            } else {
                /*信息完整，持久化数据信息   进入选票是清除此信息*/

                $this->assign("order",$dbData);
                session('order', $dbData);
                if (C('HOME_DEBUG')) {
                    echo "新增session订单:";
                    dump(session('order'));
                }
            }

        }
        $this->display();
    }


    /**
     * 获取车票订单详细
     */
    private function _getTicketDetail($addrId)
    {
        //乘客数组
        $psrIds = $_POST['psr'];

        //乘客数量
        $psrNum = count($psrIds);

        //根据折扣code获取折扣id
        $dicCode = $_POST['dicCode'];
        $map = array('dc_code' => $dicCode);
        $discountInfo = D('discount')->where($map)->find();

        //保存到数据库的信息
        $dbData = array(

            'user_id' => session('userId'),
            'od_name' => $this->getOrderId(),
            'od_creat_time' => time(),
            'od_finish_time' => '0',
            'od_ticket_num' => $psrNum,
            "od_total_price" => $this->getTotalPrice(),
            'od_sell_price' => $this->getSellPrice($discountInfo),
            'od_status' => '0',
            'od_psr_ids' => $this->getPsrIds($psrIds),
            'od_addr_id' => $addrId,
            'od_insur_price' => $this->getInsurPrice($psrNum),
            'od_discount_id' => $discountInfo['dc_id'],
            'od_get_ticket_person' => I('post.get_ticket_pseson'),
            'od_get_ticket_person_tel' => I('post.get_ticket_tel'),
            'od_pay_status' => '0',

        );
        return $dbData;
    }
}